import { recharge, onLinePay2, withdraw} from "@/api/money";

const state = {
    payAccountName: "123123",
    payAccountNum: "12312321",
    payBankName: "<ICBC></ICBC>",
    payMark: "test",
    rechargeGoodsId: '',
    type: "2",
    pro: 1,
    popShow: false,
    banKName:'',
    txData:{}
};
const mutations = {
    initData:(state)=>{
        state.banKName = '',
        state.txData= {}
    },
    SET_SP_ID: (state, rechargeGoodsId) => {
        state.rechargeGoodsId = rechargeGoodsId;
    },
    SET_popShow: (state, data) => {
        state.popShow = data;
    },
    SET_pro: (state, data) => {
        state.pro = data;
    },
    SET_txData: (state, data) => {
        state.txData.BankId = data;
    },
    SET_banKName: (state, data) => {
        state.banKName = data;
    },
};
const actions = {
    //支付里路程，拿去商品id换取充值地址然后跳转
    async pay({state}) {
        let param = {
            payAccountName:state.payAccountName,
            payAccountNum:state.payAccountNum,
            payBankName:state.payBankName,
            payMark:state.payMark,
            rechargeGoodsId:state.rechargeGoodsId,
            type:state.type
        }
        return new Promise((resolve)=>{
            recharge(param).then(res => {
                if(res.data.code==='200'){
                    let param2 = {
                        orderId: res.data.data.rechargeNo
                      };
                      onLinePay2(param2).then(res2 => {
                        resolve(res2)
                      });
                }
              });
        })
    },
    async withdraw({state}) {
        let param = {
                ...state.txData
        }
        console.log('param',param);
        return new Promise((resolve)=>{
            withdraw(param).then(res => {
                if(res.data.code==='200'){
                    resolve(res)
                }
              });
        })

    },
};

export default {
    namespaced: true,
    state,
    mutations,
    actions
};
